﻿using System;
using System.IO;
using Avalonia;
using AvaloniaApplication1.Factory;
using AvaloniaApplication1.Models;
using AvaloniaApplication1.Service;
using AvaloniaApplication1.ViewModels;
using AvaloniaApplication1.ViewModels.CardViewModel;
using AvaloniaApplication1.Views;
using AvaloniaApplication1.Views.CardView;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using SukiUI.Dialogs;

namespace AvaloniaApplication1;

internal sealed class Program
{
    public static IServiceProvider ServiceProvider { get; private set; }

    // Initialization code. Don't use any Avalonia, third-party APIs or any
    // SynchronizationContext-reliant code before AppMain is called: things aren't initialized
    // yet and stuff might break.
    [STAThread]
    public static void Main(string[] args)
    {
        var services = new ServiceCollection();
        ConfigureServices(services);
        ServiceProvider=services.BuildServiceProvider();
        BuildAvaloniaApp()
            .StartWithClassicDesktopLifetime(args);
    }

    // Avalonia configuration, don't remove; also used by visual designer.
    public static AppBuilder BuildAvaloniaApp()
    {
        return AppBuilder.Configure<App>()
            .UsePlatformDetect()
            .WithInterFont()
            .LogToTrace();
    }
    public static void ConfigureServices(IServiceCollection services)
    {
        // 创建(app settings)配置对象
        var configuration = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("AppSettings.json", optional: false, reloadOnChange: true)
            .Build();
        services.Configure<AppSettings>(configuration.GetSection("AppSettings"));
        // 注册 ViewModels
        services.AddSingleton<MainWindowViewModel>();
        services.AddSingleton<HomePageViewModel>();
        services.AddSingleton<SettingsPageViewModel>();
        services.AddSingleton<TmphumViewModel>();
        services.AddSingleton<DataGridViewModel>();
        services.AddSingleton<LineChartViewModel>();
        services.AddSingleton<DialogTestViewModel>();
        services.AddSingleton<DevicePageViewModel>();
        services.AddSingleton<DeviceCardViewModel>();
        services.AddSingleton<ISukiDialogManager, SukiDialogManager>();


        services.AddViewModelFactories();

  //      services.AddSingleton<IViewModelFactory<DeviceCardModel, DialogTestViewModel>,DialogTestViewModelFactory>();

        // services.AddSingleton<IViewModelFactory<DeviceCardModel, DeviceCardViewModel>,DeviceCardViewModelFactory>();
        // 注册 Services（可选）
        services.AddSingleton<ThemeService>();
        // services.AddSingleton<IConfigurationService, ConfigurationService>();

        // 注册 Views（如果需要通过 DI 构造 View）
        services.AddTransient<MainWindow>();
        services.AddTransient<HomePageView>();
        services.AddTransient<SettingsPageView>();
        services.AddTransient<TmphumView>();
        services.AddTransient<DataGridView>();
        services.AddTransient<LineChartView>();
        services.AddTransient<DevicePageView>();
        services.AddTransient<DeviceCardView>();
        services.AddTransient<DialogTestView>();
    }
}